home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dme / cmd3.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  8KB  |  500 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  * CMD3.C
  9.  *
  10.  *  SETFONT
  11.  *  IGNORECASE
  12.  *  SET
  13.  *  SETENV
  14.  *  UNSET
  15.  *  UNSETENV
  16.  *  CD
  17.  *  SAVECONFIG
  18.  *  FGPEN
  19.  *  TPEN
  20.  *  BGPEN
  21.  *  TITLE
  22.  *  JUSTIFY
  23.  *  UNJUSTIFY
  24.  *  MODIFIED
  25.  *  UNDELINE
  26.  */
  27.  
  28. #include "defs.h"
  29.  
  30. Prototype void do_setfont (void);
  31. Prototype void do_ignorecase (void);
  32. Prototype void do_cd (void);
  33. Prototype void do_set (void);
  34. Prototype void do_setenv (void);
  35. Prototype void do_unset (void);
  36. Prototype void do_unsetenv (void);
  37. Prototype char *getvar (char *);
  38. Prototype void loadconfig(ED *);
  39.  
  40. Prototype void do_saveconfig(void);
  41. Prototype void do_fgpen(void);
  42. Prototype void do_bgpen(void);
  43. Prototype void do_hgpen(void);
  44. Prototype void do_tpen(void);
  45. Prototype void do_justify(void);
  46. Prototype void do_title(void);
  47. Prototype void do_undeline(void);
  48. Prototype void do_unjustify(void);
  49. Prototype void do_modified(void);
  50. Prototype void do_sizewindow(void);
  51. Prototype void do_space(void);
  52.  
  53.  
  54. #define nomemory()  { memoryfail = 1; }
  55.  
  56. /*
  57.  *  SETFONT font size
  58.  */
  59.  
  60. void
  61. do_setfont()
  62. {
  63.     FONT *font = (FONT *)GetFont(av[1], (short)atoi(av[2]));
  64.     ED *ep = Ep;
  65.     if (font) {
  66.     text_sync();
  67.     if (ep->Font)
  68.         CloseFont(ep->Font);
  69.     ep->Font = font;
  70.     SetFont(ep->Win->RPort, font);
  71.     SetRast(ep->Win->RPort, 0);
  72.     RefreshWindowFrame(ep->Win);
  73.     set_window_params();
  74.     text_redisplay();
  75.     } else {
  76.     title("Unable to find font");
  77.     }
  78. }
  79.  
  80. void
  81. do_ignorecase()
  82. {
  83.     ED *ep = Ep;
  84.  
  85.     if (av[1][0]) {
  86.     switch(av[1][1] & 0x1F) {
  87.     case 'n'&0x1F:
  88.         ep->IgnoreCase = 1;
  89.         break;
  90.     case 'f'&0x1F:
  91.         ep->IgnoreCase = 0;
  92.         break;
  93.     case 'o'&0x1F:
  94.         ep->IgnoreCase = 1 - ep->IgnoreCase;
  95.         break;
  96.     }
  97.     if (ep->IgnoreCase)
  98.         title("Case InSensitive");
  99.     else
  100.         title("Case Sensitive");
  101.     }
  102. }
  103.  
  104. /*
  105.  *  av[1]
  106.  */
  107.  
  108. void
  109. do_cd()
  110. {
  111.     BPTR oldlock;
  112.     BPTR lock;
  113.  
  114.     oldlock = CurrentDir((BPTR)Ep->dirlock);
  115.     if (lock = Lock(av[1], SHARED_LOCK)) {
  116.     UnLock(CurrentDir(oldlock));
  117.     Ep->dirlock = (long)lock;
  118.     } else {
  119.     CurrentDir(oldlock);
  120.     Abortcommand = 1;
  121.     title("Unable to CD");
  122.     }
  123. }
  124.  
  125. /*
  126.  *  VARIABLE SUPPORT!
  127.  */
  128.  
  129. #define VARS    struct _VARS
  130. VARS {
  131.     MNODE   Node;
  132.     char    *Name;
  133.     char    *Str;
  134. };
  135.  
  136. static MLIST SList = { (MNODE *)&SList.mlh_Tail, NULL, (MNODE *)&SList.mlh_Head };
  137.  
  138. void
  139. do_set()
  140. {
  141.     VARS *v;
  142.     void do_unset();
  143.  
  144.     do_unset();
  145.     if (v = malloc(sizeof(VARS))) {
  146.     if (v->Name = malloc(strlen(av[1])+1)) {
  147.         if (v->Str = malloc(strlen(av[2])+1)) {
  148.         AddHead((LIST *)&SList, (NODE *)v);
  149.         strcpy(v->Name, av[1]);
  150.         strcpy(v->Str , av[2]);
  151.         return;
  152.         }
  153.         free(v->Name);
  154.     }
  155.     free(v);
  156.     }
  157.     nomemory();
  158. }
  159.  
  160. void
  161. do_setenv()
  162. {
  163.     SetDEnv(av[1], av[2]);
  164. }
  165.  
  166. void
  167. do_unset()
  168. {
  169.     VARS *v;
  170.  
  171.     for (v = (VARS *)SList.mlh_Head; v->Node.mln_Succ; v = (VARS *)v->Node.mln_Succ) {
  172.     if (strcmp(v->Name, av[1]) == 0) {
  173.         Remove((NODE *)v);
  174.         free(v->Name);
  175.         free(v->Str);
  176.         free(v);
  177.         break;
  178.     }
  179.     }
  180. }
  181.  
  182. void
  183. do_unsetenv()
  184. {
  185.     char *ptr = (char *)av[1];
  186.     char *tmp = malloc(4+strlen(ptr)+1);
  187.  
  188.     if (tmp) {
  189.     strcpy(tmp, "ENV:");
  190.     strcat(tmp, ptr);
  191.     mountrequest(0);
  192.     DeleteFile(tmp);
  193.     mountrequest(1);
  194.     free(tmp);
  195.     }
  196. }
  197.  
  198. /*
  199.  *  Search (1) internal list, (2) enviroment, (3) macros.  The variable
  200.  *  is allocated with malloc().  NULL if not found.  ENV: need not exist.
  201.  */
  202.  
  203. char *
  204. getvar(find)
  205. char *find;
  206. {
  207.     char *str = NULL;
  208.     {
  209.     VARS *v;
  210.  
  211.     for (v = (VARS *)SList.mlh_Head; v->Node.mln_Succ; v = (VARS *)v->Node.mln_Succ) {
  212.         if (strcmp(v->Name, find) == 0) {
  213.         if (str = malloc(strlen(v->Str)+1)) {
  214.             strcpy(str, v->Str);
  215.             return(str);
  216.         }
  217.         }
  218.     }
  219.     }
  220.  
  221.     mountrequest(0);
  222.     str = (char *)GetDEnv(find);
  223.     mountrequest(1);
  224.     if (str)
  225.     return(str);
  226.  
  227.     if ((str = keyspectomacro(find)) || (str = menutomacro(find))) {
  228.     char *ptr = malloc(strlen(str)+1);
  229.     if (ptr) {
  230.         strcpy(ptr, str);
  231.         return(ptr);
  232.     }
  233.     }
  234.     return(NULL);
  235. }
  236.  
  237. void
  238. do_col()
  239. {
  240.     int col;
  241.  
  242.     {
  243.     char *ptr = av[1];
  244.  
  245.     switch(*ptr) {
  246.     case '+':
  247.         col = text_colno() + atoi(ptr + 1);
  248.         if (col > 254)
  249.         col = 254;
  250.         break;
  251.     case '-':
  252.         col = text_colno() + atoi(ptr);
  253.         if (col < 0)
  254.         col = 0;
  255.         break;
  256.     default:
  257.         col = atoi(ptr) - 1;
  258.         break;
  259.     }
  260.     }
  261.     if (col > 254 || col < 0) {
  262.     Abortcommand = 1;
  263.     return;
  264.     }
  265.     while (Clen < col)
  266.     Current[Clen++] = ' ';
  267.     Current[Clen] = 0;
  268.     Ep->Column = col;
  269.     if (Ep->Column - Ep->Topcolumn >= Columns || Ep->Column < Ep->Topcolumn)
  270.     text_sync();
  271. }
  272.  
  273. void
  274. do_saveconfig()
  275. {
  276.     ED *ep = Ep;
  277.     FILE *fi;
  278.  
  279.     if (ep->iconmode == 0) {
  280.     WIN *win = ep->Win;
  281.     ep->Winx      = win->LeftEdge;
  282.     ep->Winy      = win->TopEdge;
  283.     ep->Winwidth  = win->Width;
  284.     ep->Winheight = win->Height;
  285.     }
  286.  
  287.     if (fi = fopen("s:dme.config", "w")) {
  288.     fwrite(&ep->BeginConfig, (char *)&ep->EndConfig - (char *)&ep->BeginConfig, 1, fi);
  289.     fclose(fi);
  290.     }
  291. }
  292.  
  293. void
  294. loadconfig(ep)
  295. ED *ep;
  296. {
  297.     FILE *fi;
  298.  
  299.     if (fi = fopen("s:dme.config", "r")) {
  300.     fread(&ep->BeginConfig, (char *)&ep->EndConfig - (char *)&ep->BeginConfig, 1, fi);
  301.     fclose(fi);
  302.     }
  303. }
  304.  
  305. void
  306. do_fgpen()
  307. {
  308.     ED *ep = Ep;
  309.  
  310.     ep->FGPen = atoi(av[1]);
  311. }
  312.  
  313. void
  314. do_tpen()
  315. {
  316.     ED *ep = Ep;
  317.  
  318.     ep->TPen = atoi(av[1]);
  319. }
  320.  
  321. void
  322. do_bgpen()
  323. {
  324.     ED *ep = Ep;
  325.  
  326.     ep->BGPen = atoi(av[1]);
  327. }
  328.  
  329. void
  330. do_hgpen()
  331. {
  332.     ED *ep = Ep;
  333.  
  334.     ep->HGPen = atoi(av[1]);
  335. }
  336.  
  337. /*
  338.  *  Commands submitted by Markus Wenzel
  339.  */
  340.  
  341. void
  342. do_undeline()
  343. {
  344.    do_insline();
  345.    text_load();
  346.    strcpy(Current, Deline);
  347.    text_sync();
  348.    text_displayseg(Ep->Line - Ep->Topline, 1);
  349. }
  350.  
  351.  
  352. void
  353. do_modified()
  354. {
  355.     register ED *ep = Ep;
  356.  
  357.     if (av[1][0]) {
  358.     switch(av[1][1] & 0x1F) {
  359.     case 'n' & 0x1F:
  360.         ep->Modified = 1;
  361.         break;
  362.     case 'f' & 0x1F:
  363.         ep->Modified = 0;
  364.         break;
  365.     case 'o' & 0x1F:
  366.         ep->Modified = ep->Modified ? 0 : 1;
  367.         break;
  368.     }
  369.     }
  370. }
  371.  
  372.  
  373. void
  374. do_unjustify()
  375. {
  376.     short i, j, waswhite = FALSE;
  377.     ubyte c;
  378.  
  379.  
  380.     for (i = 0; Current[i] == ' '; i++);
  381.     for (j = i; Current[i]; i++) {
  382.     c = Current[j] = Current[i];
  383.     if (c != ' ' || !waswhite)
  384.         j++;
  385.     waswhite = (c == ' ');
  386.  
  387.     }
  388.     Current[j] = 0;
  389.  
  390.     if (i != j) {
  391.     text_sync();
  392.     text_redisplaycurrline();
  393.     }
  394. }
  395.  
  396.  
  397. void
  398. do_justify()
  399. {
  400.     ED *ep = Ep;
  401.     short firstnb, lastnb, i, n, fill, c, sp;
  402.     short changed = FALSE;
  403.  
  404.  
  405.     switch(av[1][0]) {
  406.     case 'c':
  407.     break;
  408.     case 'f':
  409.     firstnb = firstns(Current);
  410.     lastnb = lastns(Current);
  411.     if (firstnb < lastnb && ep->Margin < 255) {
  412.         n = 0;
  413.         i = firstnb;
  414.         while (i <= lastnb) {
  415.         while ((c = Current[i]) && c != ' ')
  416.             i++;
  417.         if (i <= lastnb) {
  418.             n++;
  419.             while (Current[i] == ' ')
  420.             i++;
  421.         }
  422.         }
  423.         fill = ep->Margin - lastnb - 1;
  424.         i = firstnb;
  425.         Current[lastnb + 1] = 0;
  426.         if (n > 0 && fill > 0)
  427.         changed = TRUE;
  428.         while (n > 0 && fill > 0 && Current[i]) {
  429.         while ((c = Current[i]) && c != ' ')
  430.             i++;
  431.         sp = fill / n;
  432.         movmem(&Current[i], &Current[i + sp], strlen(&Current[i]) + 1);
  433.         memset(&Current[i], ' ', sp);
  434.         while (Current[i] == ' ')
  435.             i++;
  436.         fill -= sp;
  437.         n--;
  438.         }
  439.     }
  440.     break;
  441.     default:
  442.     break;
  443.     }
  444.  
  445.     if (changed) {
  446.     text_sync();
  447.     text_redisplaycurrline();
  448.     }
  449. }
  450.  
  451.  
  452. void
  453. do_title()
  454. {
  455.     static ubyte wtitle[256];
  456.  
  457.     strncpy(wtitle, av[1], 255);
  458.     wtitle[255] = 0;
  459.     title(wtitle);
  460. }
  461.  
  462. void
  463. do_space()
  464. {
  465.     ED *ep = Ep;
  466.     int insmode = ep->Insertmode;
  467.  
  468.     ep->Insertmode = 1;
  469.     text_write(" ");
  470.     ep->Insertmode = insmode;
  471. }
  472.  
  473. void                        /* MMW 1.42 */
  474. do_sizewindow()
  475. {
  476.     volatile WIN *win = Ep->Win;
  477.     struct NewWindow nw;
  478.     int mdx, mdy;
  479.  
  480.     GeometryToNW(av[1], &nw);
  481.  
  482.     if (nw.LeftEdge + nw.Width <= win->WScreen->Width &&
  483.     nw.TopEdge + nw.Height <= win->WScreen->Height &&
  484.     nw.Width >= win->MinWidth &&
  485.     nw.Height >= win->MinHeight) {
  486.  
  487.     mdx = nw.LeftEdge - win->LeftEdge;
  488.     mdy = nw.TopEdge - win->TopEdge;
  489.     if (mdx > 0)
  490.         mdx = 0;
  491.     if (mdy > 0)
  492.         mdy = 0;
  493.  
  494.     MoveWindow(win, mdx, mdy);
  495.     SizeWindow(win, nw.Width - win->Width, nw.Height - win->Height);
  496.     MoveWindow(win, nw.LeftEdge - win->LeftEdge, nw.TopEdge - win->TopEdge);
  497.     }
  498. }
  499.  
  500.